package com.shinhye.elephantsafe;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class AddressPositionActivity extends Activity {
	private final String TAG = "AddressPositionActivity";
	//菜单栏高度
	private final int barHeight = 30;
	private ImageView iv_drag;
	private SharedPreferences sp;
	private long[] mHits = new long[2];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_addresspostion);
		
		WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		@SuppressWarnings("deprecation")
		final int windowWidth = display.getWidth();
		@SuppressWarnings("deprecation")
		final int windowHeight = display.getHeight();
		final TextView iv_pos1 = (TextView) findViewById(R.id.tv_address_position1);
		final TextView iv_pos2 = (TextView) findViewById(R.id.tv_address_position2);
		
		sp = getSharedPreferences("Setting", MODE_PRIVATE);
		iv_drag = (ImageView) findViewById(R.id.iv_drag);
		
		//获取历史位置并设置给图片
		int historyX = sp.getInt("lastX", 0);
		int historyY = sp.getInt("lastY", 50);
		
		if(historyY < windowHeight / 4 - barHeight) {
			iv_pos1.setVisibility(View.INVISIBLE);
			iv_pos2.setVisibility(View.VISIBLE);
		}
		
		System.out.println(historyX + "::" + historyY);
		
		LayoutParams params = (LayoutParams) iv_drag.getLayoutParams();
		params.leftMargin = historyX;
		params.topMargin = historyY;
		iv_drag.setLayoutParams(params);
		
		//设置双击事件
		iv_drag.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i(TAG, "点击了");
				System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
		        mHits[mHits.length-1] = SystemClock.uptimeMillis();
		        if (mHits[0] >= (SystemClock.uptimeMillis()-300)) {
		        	Log.i(TAG, mHits.length + "击了");
		        	
					iv_drag.layout(windowWidth / 2 - iv_drag.getWidth() / 2,
							iv_drag.getTop(),
							windowWidth / 2 + iv_drag.getWidth() / 2,
							iv_drag.getBottom());
					
					Editor editor = sp.edit();
					editor.putInt("lastX", iv_drag.getLeft());
					editor.putInt("lastY", iv_drag.getTop());
					editor.commit();
		        }
			}
		});
		
		//设置触摸监听器
		iv_drag.setOnTouchListener(new OnTouchListener() {
			private int startX;
			private int startY;

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch(event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					startX = (int) event.getRawX();
					startY = (int) event.getRawY();
					Log.i(TAG, "按下：(X=" + startX + ";Y=" + startY + ")。");
					break;
				case MotionEvent.ACTION_MOVE:
					int newX = (int) event.getRawX();
					int newY = (int) event.getRawY();
					int dx = newX - startX;
					int dy = newY - startY;
					Log.i(TAG, "移动到：(X=" + startX + ";Y=" + startY + ")。");
					int newl = iv_drag.getLeft() + dx;
					int newt = iv_drag.getTop() + dy;
					int newr = iv_drag.getRight() + dx;
					int newb = iv_drag.getBottom() + dy;
					
					if(newl < 0 || newt < 0 || newr > windowWidth || newb > windowHeight - barHeight) {
						break;
					}
					if(newt < windowHeight / 4 - barHeight) {
						iv_pos1.setVisibility(View.INVISIBLE);
						iv_pos2.setVisibility(View.VISIBLE);
					}
					if(newb > windowHeight / 4 * 3) {
						iv_pos1.setVisibility(View.VISIBLE);
						iv_pos2.setVisibility(View.INVISIBLE);
					}
					
					iv_drag.layout(newl, newt, newr, newb);
					startX = (int) event.getRawX();
					startY = (int) event.getRawY();
					break;
				case MotionEvent.ACTION_UP:
					Log.i(TAG, "离开了");
					Editor editor = sp.edit();
					editor.putInt("lastX", iv_drag.getLeft());
					editor.putInt("lastY", iv_drag.getTop());
					editor.commit();
					break;
				}
				return false;
			}
		});
		
	}
}
